package com.yanq.liancar.activity;
/*
 *  @创建者     Administrator
 *  @创建时间   2018/9/27  10:31
 *  @描述       ${TODO}
 */


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.bean.OrderDetailsBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.QRCodeUtil;
import com.yanq.liancar.util.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderDetailsActivity extends BaseToolbarActivity {
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_serviceName)
    TextView tvServiceName;
    @BindView(R.id.tv_orderNum)
    TextView tvOrderNum;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_kefu)
    TextView tvKefu;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_shopName)
    TextView tvShopName;
    @BindView(R.id.img_QRcode)
    ImageView img_QRcode;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    private String OrderNum;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orderdetails);
        ButterKnife.bind(this);
        Utils.setStatusBar(this, true, false);
        mTvTitle.setText("订单详情");
        OrderNum = getIntent().getExtras().getString("onum");
        initOkGo();

        SpannableString spannableString1 = new SpannableString("如果您对订单有疑问，请及时联系客服");
        ForegroundColorSpan colorSpan1 = new ForegroundColorSpan(getResources().getColor(R.color.main_color));
        spannableString1.setSpan(colorSpan1, 12, spannableString1.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        ForegroundColorSpan colorSpan4 = new ForegroundColorSpan(getResources().getColor(R.color.background_content));
        spannableString1.setSpan(colorSpan4, 0, 12, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvKefu.setText(spannableString1);
    }

    /*获取CARDetails*/
    private void initOkGo() {
        OkGo.<String>get(Config.s + "llc/carServiceOrderController/queryOrderByOnum")
                .tag(this)
                .params("onum", OrderNum)
                .params("token", UserBean.getcToken(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        OrderDetailsBean bean = gson.fromJson(response.body(), OrderDetailsBean.class);
                        if (bean.isIsSuccess()) {
                            tvPrice.setText("¥ " + bean.getData().getOrder().getAllPrice());
                            if (bean.getData().getOrder().getOrderStatus() == 1) {
                                tvStatus.setText("待支付");
                                tvTime.setVisibility(View.GONE);
                                img_QRcode.setVisibility(View.GONE);
                            } else if (bean.getData().getOrder().getOrderStatus() == 2) {
                                tvStatus.setText("待接单");
                                Bitmap bitmap= QRCodeUtil.createQRCodeBitmap("OrderDetails"+bean.getData().getOrder().getOnum(),dp2px(OrderDetailsActivity.this, 150),dp2px(OrderDetailsActivity.this, 150));
                                img_QRcode.setImageBitmap(bitmap);
                            } else if (bean.getData().getOrder().getOrderStatus() == 3) {
                                tvStatus.setText("拒接单");
                                Bitmap bitmap= QRCodeUtil.createQRCodeBitmap("OrderDetails"+bean.getData().getOrder().getOnum(),dp2px(OrderDetailsActivity.this, 150),dp2px(OrderDetailsActivity.this, 150));
                                img_QRcode.setImageBitmap(bitmap);
                            } else if (bean.getData().getOrder().getOrderStatus() == 4) {
                                tvStatus.setText("待完成");
                                Bitmap bitmap= QRCodeUtil.createQRCodeBitmap("OrderDetails"+bean.getData().getOrder().getOnum(),dp2px(OrderDetailsActivity.this, 150),dp2px(OrderDetailsActivity.this, 150));
                                img_QRcode.setImageBitmap(bitmap);
                            } else if (bean.getData().getOrder().getOrderStatus() == 5) {
                                tvStatus.setText("已完成");
                                img_QRcode.setVisibility(View.GONE);
                            } else {
                                tvStatus.setText("已评价");
                                img_QRcode.setVisibility(View.GONE);
                            }
                            tvServiceName.setText(bean.getData().getOrder().getServiceName());
                            tvOrderNum.setText(bean.getData().getOrder().getOnum() + "");
                            tvPhone.setText(bean.getData().getUsername());
                            tvTime.setText(bean.getData().getOrder().getPayDate());
                            tvShopName.setText(bean.getData().getShopName());

                        }else {
                            RxToast.error(bean.getOutputMessage());
                            scrollView.setVisibility(View.GONE);
                        }
                    }
                });
    }

    @OnClick(R.id.tv_kefu)
    public void onViewClicked() {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "4008055011"));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    //dp转px
    public static int dp2px(Context context, float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal,
                context.getResources().getDisplayMetrics());
    }
}
